var webSocket = cc.Class({
    _eventList : null,
    ctor : function(url){
        this._eventList = new Array();

        this.mBufRecieve = new Uint8Array(cc.SIZE_TCP_BUFFER*10);
        this.mBufRevLength = 0;
        this.analysisMsg = new analysisMsg();
        this.loadWebSocket(url);
    },

    loadWebSocket :function(url){
        var self = this;
        var port = G.port;
        var socket = new WebSocket(url);
        socket.binaryType = "arraybuffer";
        //测试
        // var socket = this.socket = new WebSocket("ws://echo.websocket.org");
        socket.onopen =function(event){         //打开
            // GameUtils.Log("链接成功！开始发送数据");
            cc.log("connect success!");
            self.socket = socket;
            ClientNotify.send(CLIENT_NOTIFY_TYPE.CONNECT_SUCCESS, "");
        };
        socket.onmessage = function(event){     //响应
            var d = event.data;
            self.onmessage(d);
        };
        socket.onerror = function(event) {   // 错误
            cc.log("发生错误！");
            // G.event.dispatchEvent(EVENT.L_NETSTATUS ,"");
        };
        socket.onclose = function(event) {   // 关闭
            cc.log("链接关闭！");
            self.socket = null;
            ClientNotify.send(CLIENT_NOTIFY_TYPE.CONNECT_DISCONNECT, "");
        };
    },

    //发送数据
    sendClass:function(main, sub, Obj){
        if(LOG_NET_DATA){
            console.log('sendClass===main:'+ main + '   sub:' + sub);
        }
        var objSize = 0;
        if(Obj) objSize = gCByte.GetSize(Obj);
        var SendData = new Uint8Array(cc.TCP_HEAD_SIZE + objSize);

        //填写信息头
        var tcpHead = new TCP_Head();
        tcpHead.TCPInfo.cbDataKind = cc.SOCKET_VER;
        tcpHead.TCPInfo.wPacketSize = SendData.length;
        tcpHead.CommandInfo.wMainCmdID = main;
        tcpHead.CommandInfo.wSubCmdID = sub;

        gCByte.Str2Bytes(tcpHead, SendData);
        if(objSize)gCByte.Str2Bytes(Obj, SendData,cc.TCP_HEAD_SIZE);

        //加密数据
        var wSendSize = CodeMsg.EncryptBufferS(SendData, SendData.length);
        // console.log("==========sendClass===============" + SendData + "  " + this.socket);
        this.socket && this.socket.send(SendData);
    },

    send:function(main, sub, pData, wDataSize){
        //效验大小
        if (wDataSize > cc.SIZE_TCP_BUFFER) return false;
        if(LOG_NET_DATA){
            console.log('send===main:'+ main + '   sub:' + sub );
        }
        var tcpHead = new TCP_Head();
        var SendData = new Uint8Array(cc.TCP_HEAD_SIZE+wDataSize);

        //填写信息头
        tcpHead.TCPInfo.cbDataKind = cc.SOCKET_VER;
        tcpHead.TCPInfo.wPacketSize = SendData.length;
        tcpHead.CommandInfo.wMainCmdID = main;
        tcpHead.CommandInfo.wSubCmdID = sub;

        gCByte.Str2Bytes(tcpHead,SendData);

        for(var i = 0;i < wDataSize;i++){
            SendData[i+cc.TCP_HEAD_SIZE] = pData[i];
        }

        //加密数据
        var wSendSize = CodeMsg.EncryptBufferS(SendData, SendData.length);
        this.socket && this.socket.send(SendData);
    },

    onmessage:function(data){
        var dataArray = new Uint8Array(data);

        for(var i=0;i<dataArray.length;i++){
            this.mBufRecieve[this.mBufRevLength+i] = dataArray[i];
        }
        // 接收长度增加
        this.mBufRevLength += dataArray.length;

        // 尝试解包
        var iUnpackIndex = 0;
        var isReaded = false;
        var iDstLength = cc.SIZE_PACK_INFO;

        while (this.mBufRevLength >= cc.TCP_HEAD_SIZE  && (this.mBufRevLength < 65262)) {
            iDstLength = gCByte.r2(this.mBufRecieve,2);
            if (this.mBufRevLength < iDstLength) return ;
            var cbDataBuffer = this.mBufRecieve.slice(0,iDstLength);
            this.mBufRevLength -= iDstLength;
            for(var i=0;i<this.mBufRevLength;i++){
                this.mBufRecieve[i] = this.mBufRecieve[iDstLength + i];
            }

            // 解包数据并通知调用
            this.unpack(cbDataBuffer, iUnpackIndex, iDstLength);
        }
    },

    unpack:function (data,start,length){
        var wRealySize = 0;
        // 解密
        if ((data[start] & gQPCipher.getCipherMode()) > 0){
            wRealySize = CodeMsg.CrevasseBufferS(data, length);
        }

          //填写信息头
        var tcpHead = new TCP_Head();
        gCByte.Bytes2Str(tcpHead,data);

        var cbData = new Uint8Array(length-cc.TCP_HEAD_SIZE);
        for(var i=0;i<length-cc.TCP_HEAD_SIZE;i++){
            cbData[i] = data[i + cc.TCP_HEAD_SIZE];
        }
        if (tcpHead.CommandInfo.wMainCmdID == 0){
            if(tcpHead.CommandInfo.wSubCmdID == 1) {
                // this.send(0, 1, data, length);
            }else if(tcpHead.CommandInfo.wSubCmdID == SUB_KN_CLIENT_HEART){
                if (this.mISocketEngineSink != null) this.mISocketEngineSink.OnSocketHeart();
            }
        }else{
            console.log('tcpHead wMainCmdID===' + tcpHead.CommandInfo.wMainCmdID + "   wSubCmdID===" + tcpHead.CommandInfo.wSubCmdID);
            this.analysisMsg.onEventTCPSocketRead(tcpHead.CommandInfo.wMainCmdID,tcpHead.CommandInfo.wSubCmdID,  cbData, cbData.length);
        }
        return true;
    },

    close : function(){
        if(cc.sys.isObjectValid(this.socket))
            this.socket.close();
            this.socket = null;
    },

    onExit:function(){
        this.socket.close();
        this._super();
    }
});